package com.tenement.service.impl;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.tenant.TenantManager;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tenement.common.GlobalException;
import com.tenement.dto.SelectDto;
import com.tenement.entity.Properties;
import com.tenement.entity.UserTenement;
import com.tenement.enums.RespBusiness;
import com.tenement.mapper.PropertiesMapper;
import com.tenement.mapper.UserMapper;
import com.tenement.mapper.UserTenementMapper;
import com.tenement.service.PropertiesService;
import com.tenement.vo.QueryVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.tenement.entity.table.PropertiesTableDef.PROPERTIES;
import static com.tenement.entity.table.UserTableDef.USER;

/**
 *  服务层实现。
 *
 * @author BertXie
 * @since 1.0.1
 */
@Service
@RequiredArgsConstructor
public class PropertiesServiceImpl extends ServiceImpl<PropertiesMapper, Properties> implements PropertiesService{

    private final PropertiesMapper propertiesMapper;

    private final UserMapper userMapper;

    private final UserTenementMapper userTenementMapper;

    @Override
    public Page<Properties> list(QueryVo queryVo) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .from(PROPERTIES)
                .select(
                    PROPERTIES.ID, PROPERTIES.PROPERTY_NAME, PROPERTIES.COMMUNITY_NAME,
                    PROPERTIES.ADDRESS, PROPERTIES.CONTACT_PERSON, PROPERTIES.REGION,
                    PROPERTIES.BUILDING_AREA, PROPERTIES.LATITUDE, PROPERTIES.LONGITUDE,
                    PROPERTIES.WATER_FEE, PROPERTIES.ELECTRICITY_FEE, PROPERTIES.GAS_FEE,
                    PROPERTIES.PROPERTY_FEE, PROPERTIES.ESTABLISHED_DATE, PROPERTIES.COMMUNITY_DESCRIPTION,
                    PROPERTIES.PROPERTY_TYPE, PROPERTIES.CREATE_TIME, PROPERTIES.UPDATE_TIME,
                    PROPERTIES.EMAIL, PROPERTIES.PHONE_NUMBER
                )
                .where(
                        PROPERTIES.PROPERTY_NAME.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord()))
                                .or(PROPERTIES.COMMUNITY_NAME.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(PROPERTIES.ADDRESS.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(PROPERTIES.CONTACT_PERSON.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord()))))
                .orderBy(PROPERTIES.UPDATE_TIME,false);
        return propertiesMapper.paginateAs(queryVo.getPage(), queryVo.getSize(), queryWrapper, Properties.class);
    }

    @Override
    public boolean delete(Long id) {
        Long hasAdmin = TenantManager.withoutTenantCondition(() ->
                userMapper.selectCountByQuery(QueryWrapper.create().from(USER).where(USER.PROPERTY_ID.eq(id)))
        );
        if (hasAdmin != 0L) {
            throw new GlobalException(RespBusiness.HAS_ADMIN_PROPERTY);
        }
        return removeById(id);
    }

    @Override
    public List<SelectDto> bindSelect(String keyWord) {
        return propertiesMapper.bindSelect(keyWord);
    }

    @Override
    public int operator(Properties properties, Long userId) {
        int status = 0;
        if (properties.getId() == null) {
            status = propertiesMapper.insertSelective(properties);
            if(status > 0) {
                try {
                    userTenementMapper.insertSelective(new UserTenement(userId,properties.getId()));
                    return status;
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
            return propertiesMapper.update(properties);
        }
        return 0;
    }


}
